/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import controller.Forms.AccountantForm;
import dao.AccountantDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Accountant;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class RegisterAccountantAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            User u = (User) request.getSession().getAttribute("User");

            if (u == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {
                AccountantForm contForm = (AccountantForm) form;
                Accountant contador = new Accountant();

                contador.setTxtNome(contForm.getTxtNome());
                contador.setTxtCpf(contForm.getTxtCpf().replace(".","").replace("-","").trim());
                contador.setTxtCrc(contForm.getTxtCrc());
                contador.setLstUF(contForm.getlstUF());
                contador.setLstAssinatura(contForm.getLstAssinatura());

                AccountantDAO contadorDAO = new AccountantDAO();

                contadorDAO.create(contador);
                contadorDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }

        } catch (Exception e) {
            System.out.println("CADASTRAR_CONTADORACTION-" + e.getMessage());
            return mapping.findForward("erroAlias");
        }

    }
}
